﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Bussiness.Entity;
using AF_ORM.CORE;
using Bussiness.Manager;

namespace DSC
{
    public partial class frmSearch : Form
    {
        public delegate void AdvanceSearch(object param);
        public AdvanceSearch advanceSearch;
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Enter) // thoat
            {
                LoadGridSearch();
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
        public frmSearch()
        {
            InitializeComponent();
            // default data
            txtDenNgay.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59);
            txtTuNgay.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
            checkThoiGianThucHien.Checked = false;
            checkTongTien.Checked = false;
            txtTongTienTu.Enabled = false;
            txTongTienDen.Enabled = false;
            txtPhutThucHienTu.Enabled = false;
            txtPhutThucHienDen.Enabled = false;
            txtTuKhoa.CharacterCasing = CharacterCasing.Upper;
            txtTuSoPhieu.CharacterCasing = CharacterCasing.Upper;
            txtDenSoPhieu.CharacterCasing = CharacterCasing.Upper;
            //Load model
            PT_HondaModelEntity obj = new PT_HondaModelEntity();
            obj.IsDeleted = false;
            obj.IsActive = true;
            SelectOptions option = new SelectOptions();
            option.AddSortField("OrderIndex", true);

            DataTable tbl = PT_HondaModelManager.CreateInstant().SelectWithCondition(obj, option);
            cmbLoaiXe.Items.Add("Tất cả");
            for (int i = 0; i < tbl.Rows.Count; i++)
            {
                cmbLoaiXe.Items.Add(tbl.Rows[i]["ModelCode"].ToString());
            }
            // Load Nhan vien
            //lay thong tin nguoi sua chua
            tbl = PT_AccountManager.CreateInstant().GetListAccount3(0);
            DataRow dr = tbl.NewRow();
            dr["UserId"] = 0;
            dr["UserName"] = "Tất cả";
            tbl.Rows.InsertAt(dr, 0);
            cmbNVSC.Items.Clear();
            cmbNVSC.DataSource = tbl;
            cmbNVSC.DisplayMember = "UserName";
            cmbNVSC.ValueMember = "UserId";

            cmbNVTiepNhan.DataSource = tbl.Copy();
            cmbNVTiepNhan.DisplayMember = "UserName";
            cmbNVTiepNhan.ValueMember = "UserId";

            cmbNVThuNgan.DataSource = tbl.Copy();
            cmbNVThuNgan.DisplayMember = "UserName";
            cmbNVThuNgan.ValueMember = "UserId";

            cmbNVKTXeRa.DataSource = tbl.Copy();
            cmbNVKTXeRa.DisplayMember = "UserName";
            cmbNVKTXeRa.ValueMember = "UserId";

            this.FormClosing += new FormClosingEventHandler(Search_FormClosing);
        }

        void Search_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
        }

        private void HandleCheckChange()
        {
            if (checkHoTenKH.Checked == true || checkBienSo.Checked == true || checkSDT.Checked == true)
                txtTuKhoa.Enabled = true;
            else
                txtTuKhoa.Enabled = false;
        }
        private bool ValidateData()
        {
            if (txtDenNgay.Value.CompareTo(txtTuNgay.Value) < 0)
            {
                MessageBox.Show("Từ ngày phải nhỏ hơn đến ngày");
                return false;
            }
            //TimeSpan c = txtDenNgay.Value.Subtract(txtTuNgay.Value);
            //if (c.TotalDays > 90)
            //{
            //    MessageBox.Show("Chỉ chọn trong khoảng thời gian 3 tháng trở xuống");
            //    return false;
            //}
            return true;
        }
        public void LoadGridSearch()
        {
            if (ValidateData())
            {
                string tenKH = "";
                string bienSo = "";
                string dienThoai = "";
                if (checkHoTenKH.Checked == true)
                    tenKH = txtTuKhoa.Text.Trim();
                else if (checkBienSo.Checked == true)
                    bienSo = txtTuKhoa.Text.Trim();
                else if (checkSDT.Checked == true)
                    dienThoai = txtTuKhoa.Text;
                string tuPhieu = txtTuSoPhieu.Text.Trim();
                string denPhieu = txtDenSoPhieu.Text.Trim();
                DateTime TuNgay = txtTuNgay.Value;
                DateTime DenNgay = txtDenNgay.Value;
                int nvSuaChua = int.Parse(cmbNVSC.SelectedValue.ToString());
                int nvTiepNhan = int.Parse(cmbNVTiepNhan.SelectedValue.ToString());
                int nvKiemTra = int.Parse(cmbNVKTXeRa.SelectedValue.ToString());
                int nvThuNgan = int.Parse(cmbNVThuNgan.SelectedValue.ToString());
                string phanloaidichvu = "";
                if (checkDVRap.Checked == true)
                {
                    if (!string.IsNullOrEmpty(phanloaidichvu))
                    {
                        phanloaidichvu += ",";
                    }
                    phanloaidichvu += "RB";
                }
                if (checkDVKTDK.Checked == true)
                {
                    if (!string.IsNullOrEmpty(phanloaidichvu))
                    {
                        phanloaidichvu += ",";
                    }
                    phanloaidichvu += "KTDK";
                }
                if (checkDVGCN.Checked == true)
                {
                    if (!string.IsNullOrEmpty(phanloaidichvu))
                    {
                        phanloaidichvu += ",";
                    }
                    phanloaidichvu += "GCN";
                }
                if (checkDVSC.Checked == true)
                {
                    if (!string.IsNullOrEmpty(phanloaidichvu))
                    {
                        phanloaidichvu += ",";
                    }
                    phanloaidichvu += "SC";
                }
                string giamGiaDichVu = "";
                if (checkGGThongThuong.Checked == true)
                {
                    if (!string.IsNullOrEmpty(giamGiaDichVu))
                    {
                        giamGiaDichVu += ",";
                    }
                    giamGiaDichVu += "TT";
                }
                if (checkGGBaoHanh.Checked == true)
                {
                    if (!string.IsNullOrEmpty(giamGiaDichVu))
                    {
                        giamGiaDichVu += ",";
                    }
                    giamGiaDichVu += "BH";
                }
                if (checkGGThienChi.Checked == true)
                {
                    if (!string.IsNullOrEmpty(giamGiaDichVu))
                    {
                        giamGiaDichVu += ",";
                    }
                    giamGiaDichVu += "TC";
                }
                if (checkGGGiamGia.Checked == true)
                {
                    if (!string.IsNullOrEmpty(giamGiaDichVu))
                    {
                        giamGiaDichVu += ",";
                    }
                    giamGiaDichVu += "GG";
                }
                if (checkGGKhuyenMai.Checked == true)
                {
                    if (!string.IsNullOrEmpty(giamGiaDichVu))
                    {
                        giamGiaDichVu += ",";
                    }
                    giamGiaDichVu += "KM";
                }
                string phanLoaiPhuTung = "";
                if (checkPTTrong.Checked == true)
                {
                    if (!string.IsNullOrEmpty(phanLoaiPhuTung))
                    {
                        phanLoaiPhuTung += ",";
                    }
                    phanLoaiPhuTung += "0";
                }
                if (checkPTNgoai.Checked == true)
                {
                    if (!string.IsNullOrEmpty(phanLoaiPhuTung))
                    {
                        phanLoaiPhuTung += ",";
                    }
                    phanLoaiPhuTung += "1";
                }
                if (checkPTTuMua.Checked == true)
                {
                    if (!string.IsNullOrEmpty(phanLoaiPhuTung))
                    {
                        phanLoaiPhuTung += ",";
                    }
                    phanLoaiPhuTung += "2";
                }
                string giamGiaphuTung = "";
                if (checkPTGGThongThuong.Checked == true)
                {
                    if (!string.IsNullOrEmpty(giamGiaphuTung))
                    {
                        giamGiaphuTung += ",";
                    }
                    giamGiaphuTung += "TT";
                }
                if (checkPTGGBaoHanh.Checked == true)
                {
                    if (!string.IsNullOrEmpty(giamGiaphuTung))
                    {
                        giamGiaphuTung += ",";
                    }
                    giamGiaphuTung += "BH";
                }
                if (checkPTGGThienChi.Checked == true)
                {
                    if (!string.IsNullOrEmpty(giamGiaphuTung))
                    {
                        giamGiaphuTung += ",";
                    }
                    giamGiaphuTung += "TC";
                }
                if (checkPTGGGiamGia.Checked == true)
                {
                    if (!string.IsNullOrEmpty(giamGiaphuTung))
                    {
                        giamGiaphuTung += ",";
                    }
                    giamGiaphuTung += "GG";
                }
                if (checkPTGGKhuyenMai.Checked == true)
                {
                    if (!string.IsNullOrEmpty(giamGiaphuTung))
                    {
                        giamGiaphuTung += ",";
                    }
                    giamGiaphuTung += "KM";
                }
                string phanLoaiXe = cmbLoaiXe.Text.Replace("Tất cả,", "");
                decimal tongTienTu = -1;
                decimal tongTienDen = -1;
                if (checkTongTien.Checked == true)
                {
                    tongTienTu = txtTongTienTu.Value * 1000;
                    tongTienDen = txTongTienDen.Value * 1000;
                }

                int soPhutThucHienTu = -1;
                int soPhutThucHienDen = -1;
                if (checkThoiGianThucHien.Checked == true)
                {
                    soPhutThucHienTu = (int)txtPhutThucHienTu.Value;
                    soPhutThucHienDen = (int)txtPhutThucHienDen.Value;
                }
                int isFinished = -1;
                if (advanceSearch != null)
                {
                    object[] param = new object[] { isFinished, tenKH, bienSo, dienThoai, tuPhieu, denPhieu, TuNgay, DenNgay, nvSuaChua, nvTiepNhan, nvKiemTra, nvThuNgan, phanloaidichvu, giamGiaDichVu, phanLoaiPhuTung, checkPTNhot.Checked, giamGiaphuTung, phanLoaiXe, tongTienTu, tongTienDen, soPhutThucHienTu, soPhutThucHienDen };
                    advanceSearch(param);
                }
            }
        }
        private void btSearch_Click(object sender, EventArgs e)
        {
            LoadGridSearch();
            this.Hide();
        }

        private void checkHoTenKH_CheckedChanged(object sender, EventArgs e)
        {
            HandleCheckChange();
        }

        private void checkBienSo_CheckedChanged(object sender, EventArgs e)
        {
            HandleCheckChange();
        }

        private void checkSDT_CheckedChanged(object sender, EventArgs e)
        {
            HandleCheckChange();
        }

        private void checkTongTien_CheckedChanged(object sender, EventArgs e)
        {
            if (checkTongTien.Checked == false)
            {
                txtTongTienTu.Enabled = false;
                txTongTienDen.Enabled = false;
            }
            else
            {
                txtTongTienTu.Enabled = true;
                txTongTienDen.Enabled = true;
            }
        }

        private void checkThoiGianThucHien_CheckedChanged(object sender, EventArgs e)
        {
            if (checkThoiGianThucHien.Checked == false)
            {
                txtPhutThucHienTu.Enabled = false;
                txtPhutThucHienDen.Enabled = false;
            }
            else
            {
                txtPhutThucHienTu.Enabled = true;
                txtPhutThucHienDen.Enabled = true;
            }
        }

        private void btThoat_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
    }
}
